"use strict";
const db = uniCloud.database();

/**
 * 获取 MBTI 题库
 * @param {Object} event - { level: 'low' | 'medium' | 'high' }
 */
exports.main = async (event, context) => {
  const { level = "medium" } = event || {};
  try {
    const collection = db.collection("mbti_questions");
    const { data } = await collection.where({ level }).limit(1).get();
    if (Array.isArray(data) && data.length > 0) {
      return { code: 0, data: data[0] };
    }
  } catch (error) {
    // 不中断，走默认回退
  }
  // 默认回退结构，前端会切到本地静态题库
  return { code: 0, data: { level, version: 1, questions: [] } };
}; 